package com.core.zyyz.util;


import com.core.zyyz.amc.modules.domain.AmcCodeManage;
import com.core.zyyz.amc.modules.domain.BaseDictionary;
import com.core.zyyz.amc.modules.service.AmcCodeManageService;
import com.core.zyyz.constants.FunctionCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@Component
public class CodeMakerUtil {

    private static CodeMakerUtil codeMakerUtil;

    @Autowired
    private AmcCodeManageService amcCodeManagerService;

    @PostConstruct
    public void init() {
        codeMakerUtil = this;
    }

    public String bussCode(String functionCode) {
        AmcCodeManage amcCodeManage = codeMakerUtil.amcCodeManagerService.selectAmcCodeManageByFunctionCode(functionCode);
        BaseDictionary bd = DictUtils.get("amc", "code_style", amcCodeManage.getCodeStyle());
        SimpleDateFormat df = new SimpleDateFormat(bd.getLabel());
        return createCode(amcCodeManage.getCodePrefix(), df.format(new Date()), amcCodeManage.getCodeSequenceLength());
    }

    /**
     * 通过功能+维度值获取编码
     *
     * @param functionCode
     * @param code
     * @return
     */
    public String bussCode(String functionCode, String code) {
        AmcCodeManage amcCodeManage = codeMakerUtil.amcCodeManagerService.selectAmcCodeManageByFunctionCode(functionCode);
        BaseDictionary bd = DictUtils.get("amc", "code_style", amcCodeManage.getCodeStyle());
        StringBuffer preFix = new StringBuffer(amcCodeManage.getCodePrefix());
        if (FunctionCode.ASSETLIBRARY.equals(functionCode)) {
            if (code.startsWith("01")) {
                preFix.append("_").append("BLZC").append("_");
            }
            if (code.startsWith("02")) {
                preFix.append("_").append("ZQZC").append("_");
            }
            if (code.startsWith("03")) {
                preFix.append("_").append("FWZC").append("_");
            }
            if (code.startsWith("04")) {
                preFix.append("_").append("GQZC").append("_");
            }
            if (code.startsWith("07")) {
                preFix.append("_").append("RZZC").append("_");
            }
            // 解决基金类型的编号规则错误
            if (code.startsWith("11")) {
                preFix.append("_").append("JJCP").append("_");
            }
        }
        SimpleDateFormat df = new SimpleDateFormat(bd.getLabel());
        return createCode(preFix.toString(), df.format(new Date()), amcCodeManage.getCodeSequenceLength());
    }

    public String createCode(String prefix, String dateStr, int length) {
        StringBuffer code = new StringBuffer();
        if ((prefix != null && !prefix.equals("")) || (dateStr != null && !dateStr.equals(""))) {
            if (prefix != null && !prefix.equals("")) {
                code.append(prefix);
            }
            if (dateStr != null && !dateStr.equals("")) {
                code.append(dateStr);
            }
            HashMap<String, Object> param = new HashMap<String, Object>();
            param.put("prefix", prefix);
            param.put("dateStr", dateStr);
            synchronized (code) {
                Integer seq = codeMakerUtil.amcCodeManagerService.getSeq(param);
                if (seq == null) {
                    seq = 1;
                    param.put("seq", seq);
                    codeMakerUtil.amcCodeManagerService.insertSysCodeFactory(param);
                } else {
                    seq++;
                    param.put("seq", seq);
                    codeMakerUtil.amcCodeManagerService.updateCodeSeq(param);
                }
                if (length > 0) {
                    code.append(String.format("%0" + length + "d", seq));
                } else {
                    code.append(seq);
                }
            }
        }
        return code.toString();
    }
}
